﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using GroundWater.Data;
using GroundWater.BusinessActions;

using WaterSourceTable = GroundWater.Data.InfoDataSet.WaterSourceDataTable;
using WaterSourceRow = GroundWater.Data.InfoDataSet.WaterSourceRow;

namespace GroundWater.Basedata
{
    public partial class WaterSource : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUtil.Validate(this, "水源地信息维护");
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs args)
        {
            GridView gridView = (GridView)sender;

            if (gridView.SortExpression.Length > 0)
            {
                int cellIndex = -1;
                foreach (DataControlField field in gridView.Columns)
                {
                    if (field.SortExpression == gridView.SortExpression)
                    {
                        cellIndex = gridView.Columns.IndexOf(field);
                        break;
                    }
                }

                if (cellIndex > -1)
                {
                    if (args.Row.RowType == DataControlRowType.Header)
                    {
                        //  this is a header row,
                        //  set the sort style
                        args.Row.Cells[cellIndex].CssClass +=
                            (gridView.SortDirection == SortDirection.Ascending
                            ? " sortasc" : " sortdesc");
                    }
                }
            }

            if (args.Row.RowType == DataControlRowType.DataRow)
            {
                if (args.Row.RowIndex > -1)
                {
                    if (args.Row.Cells[8].Controls.Count > 0)
                    {
                        LinkButton btnDelete = args.Row.Cells[8].Controls[0] as LinkButton;
                        if (btnDelete != null && btnDelete.Text == "删除")
                        {
                            btnDelete.OnClientClick = "return confirm('您是否确认删除此项？');";
                        }
                    }
                }
            }
        }

        protected void ods_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            e.InputParameters["id"] = e.InputParameters["Code"];
            e.InputParameters.Remove("Code");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            GridViewRow row = ((LinkButton)sender).Parent.Parent as GridViewRow;
            string userId = this.gv.DataKeys[row.RowIndex].Value.ToString();
            SetFields(userId);

            updPnl.Update();
            txtCode.Enabled = false;

            mdlPopup.Show();
        }

        private void SetFields(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                WaterSourceRow row = GroundWater.BusinessActions.WaterSource.GetById(id, false);
                txtCode.Text = row.Code;
                txtName.Text = row.Name;
                if (!row.IsGradeNull())
                {
                    txtGrade.Text = row.Grade;
                }

                if (!row.IsAdministrativeDivisionsCodeNull())
                {
                    txtAdministrativeDivisionsCode.Text = row.AdministrativeDivisionsCode;
                }

                if (!row.IsResourceRegionCodeNull())
                {
                    txtResourceRegionCode.Text = row.ResourceRegionCode;
                }
                if (!row.IsFunctionRegionCodeNull())
                {
                    txtFunctionRegionCode.Text = row.FunctionRegionCode;
                }
                if (!row.IsProjectScaleNull())
                {
                    txtProjectScale.Text = row.ProjectScale;
                }
                if (!row.IsBuildStateNull())
                {
                    txtBuildState.Text = row.BuildState;
                }
                if (!row.IsProductDateNull())
                {
                    txtProductDate.Text = row.ProductDate.ToString("yyyy-MM-dd");
                }
                if (!row.IsProjectRuningStateNull())
                {
                    txtProjectRuningState.Text = row.ProjectRuningState;
                }

                if (!row.IsContinueStateNull())
                {
                    txtContinueState.Text = row.ContinueState;
                }
                if (!row.IsSupplyPopulationNull())
                {
                    txtSupplyPopulation.Text = row.SupplyPopulation.ToString();
                }
                if (!row.IsYearlyQualityNull())
                {
                    txtYearlyQuality.Text = row.YearlyQuality.ToString();
                }
                if (!row.IsDailyQualityNull())
                {
                    txtDailyQuality.Text = row.DailyQuality.ToString();
                }
                if (!row.IsAdministrationAgencyNull())
                {
                    txtAdministrationAgency.Text = row.AdministrationAgency;
                }
                if (!row.IsAdministrationNull())
                {
                    txtAdministration.Text = row.Administration;
                }
                if (!row.IsNoteNull())
                {
                    txtNote.Text = row.Note;
                }

                this.ViewState["WaterSourceId"] = id;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtCode.Text.Trim().Length == 0 || txtName.Text.Length == 0) return;
            string id = this.ViewState["WaterSourceId"] as String;



            InfoDataSet s = GetFields(id);
            if (s == null)
            {
                //
                lblJavaScript.Text = "<script type=\"text/javascript\">alert('已存在代码为" + txtCode.Text.Trim() +  " 的水源地。');</script>";
                return;
            }

            GroundWater.BusinessActions.WaterSource.Update(s, false, true, new string[] { s.WaterSource.TableName });

            gv.DataBind();

            updPanel.Update();
            this.ViewState.Remove("WaterSourceId");

            ClearFields();
        }

        private void ClearFields()
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtGrade.Text = "";
            txtAdministrativeDivisionsCode.Text = "";
            txtResourceRegionCode.Text = "";
            txtFunctionRegionCode.Text = "";
            txtProjectScale.Text = "";
            txtBuildState.Text = "";
            txtProductDate.Text = "";
            txtProjectRuningState.Text = "";
            txtContinueState.Text = "";
            txtSupplyPopulation.Text = "";
            txtYearlyQuality.Text = "";
            txtDailyQuality.Text = "";
            txtAdministrationAgency.Text = "";
            txtAdministration.Text = "";
            txtNote.Text = "";
        }

        private InfoDataSet GetFields(string id)
        {
            WaterSourceRow src = null;

            if (String.IsNullOrEmpty(id))
            {
                WaterSourceRow row = GroundWater.BusinessActions.WaterSource.GetById(txtCode.Text.Trim(), false);
                if (row != null)
                {
                    return null;
                }

                src = (new InfoDataSet()).WaterSource.NewWaterSourceRow();
                src.Code = "";
                src.Name = "";
                src.Grade = "";
                src.AdministrativeDivisionsCode = "";
                src.ResourceRegionCode = "";
                src.FunctionRegionCode = "";
                src.ProjectScale = "";
                src.BuildState = "";
                src.ProductDate = DateTime.Parse("1900-01-01");
                src.ProjectRuningState = "";
                src.ContinueState = "";
                src.SupplyPopulation = 0m;
                src.YearlyQuality = 0m;
                src.DailyQuality = 0m;
                src.AdministrationAgency = "";
                src.Administration = "";
                src.Note = "";

            }
            else
            {
                // update
                src = GroundWater.BusinessActions.WaterSource.GetById(id, false);
            }
            src.Code = txtCode.Text;
            src.Name = txtName.Text;
            src.Grade = txtGrade.Text;
            src.AdministrativeDivisionsCode = txtAdministrativeDivisionsCode.Text;
            src.ResourceRegionCode = txtResourceRegionCode.Text;
            src.FunctionRegionCode = txtFunctionRegionCode.Text;
            src.ProjectScale = txtProjectScale.Text;
            src.BuildState = txtBuildState.Text;
            src.ProductDate = DateTime.Parse(txtProductDate.Text);
            src.ProjectRuningState = txtProjectRuningState.Text;
            src.ContinueState = txtContinueState.Text;
            src.SupplyPopulation = Convert.ToDecimal(txtSupplyPopulation.Text);
            src.YearlyQuality = Convert.ToDecimal(txtYearlyQuality.Text);
            src.DailyQuality = Convert.ToDecimal(txtDailyQuality.Text);
            src.AdministrationAgency = txtAdministrationAgency.Text;
            src.Administration = txtAdministration.Text;
            src.Note = txtNote.Text;


            if (String.IsNullOrEmpty(id))
            {
                src.Table.Rows.Add(src);
            }
            else
            {

            }

            return src.Table.DataSet as InfoDataSet;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            txtCode.Enabled = true;

            ClearFields();
            mdlPopup.Show();
        }
    }
}
